<?php


namespace app\index\controller;


use app\service\TaskService;
use think\Db;

class BizSetting extends Common
{
    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 查询
     */
    function scaleSettings()
    {
        # 查询门店提成比例
        $info = Db::table('biz')
            ->field('negative_price,commission_platservice,commission_platpro,commission_selfservice,commission_selfpro,open_recommond_commission,
            recommond_commission_platservice,recommond_commission_platpro,recommond_commission_selfservice,recommond_commission_selfpro')
            ->where(array('id' => $this->bizId))
            ->find();
        if ($info['negative_price'] == 0) {
            # 是否开启差评处罚 关闭
            $info['negative_status'] = 2;
        } else {
            # 开启
            $info['negative_status'] = 1;
        }
        $info_show = array(
            'negative_price_show' => ($info['negative_price']),
            'commission_platservice_show' => ($info['commission_platservice'] * 100) . '%',
            'commission_platpro_show' => ($info['commission_platpro'] * 100) . '%',
            'commission_selfservice_show' => ($info['commission_selfservice'] * 100) . '%',
            'commission_selfpro_show' => ($info['commission_selfpro'] * 100) . '%',
            'recommond_commission_platservice_show' => ($info['recommond_commission_platservice'] * 100) . '%',
            'recommond_commission_platpro_show' => ($info['recommond_commission_platpro'] * 100) . '%',
            'recommond_commission_selfservice_show' => ($info['recommond_commission_selfservice'] * 100) . '%',
            'recommond_commission_selfpro_show' => ($info['recommond_commission_selfpro'] * 100) . '%',
        );
        # 差评处罚金额数组
        $negative_price_arr = [
            ['label' => 10, 'value' => 10],
            ['label' => 20, 'value' => 20],
            ['label' => 30, 'value' => 30],
            ['label' => 40, 'value' => 40],
            ['label' => 50, 'value' => 50]
        ];
        # 门店服务提成比例
        $bizservice_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 门店商品提成比例
        $bizpro_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 临时服务提成比例
        $selfservice_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 临时商品提成比例
        $selfpro_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 门店服务推荐提成比例
        $recommend_bizservice_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 门店商品推荐提成比例
        $recommend_bizpro_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 临时服务推荐提成比例
        $recommend_selfservice_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        # 临时商品推荐提成比例
        $recommend_selfpro_arr = [
            ['label' => '5%', 'value' => 0.05],
            ['label' => '10%', 'value' => 0.1],
            ['label' => '15%', 'value' => 0.15],
            ['label' => '20%', 'value' => 0.2]
        ];
        return array('status' => true, 'info' => $info, 'info_show' => $info_show, 'negative_price_arr' => $negative_price_arr, 'bizservice_arr' => $bizservice_arr, 'bizpro_arr' => $bizpro_arr,
            'selfservice_arr' => $selfservice_arr, 'selfpro_arr' => $selfpro_arr, 'recommend_bizservice_arr' => $recommend_bizservice_arr, 'recommend_bizpro_arr' => $recommend_bizpro_arr,
            'recommend_selfservice_arr' => $recommend_selfservice_arr, 'recommend_selfpro_arr' => $recommend_selfpro_arr);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 保存
     */
    function scaleSettings_save()
    {
        $info = input('post.info');
        if (empty($info)) {
            return array('status' => false, 'msg' => '系统错误');
        }
        if ($info['open_recommond_commission'] == 2) {
            $info['recommond_commission_platservice'] = 0;
            $info['recommond_commission_platpro'] = 0;
            $info['recommond_commission_selfservice'] = 0;
            $info['recommond_commission_selfpro'] = 0;
        }
        if ($info['negative_status'] == 2) {
            $info['negative_price'] = 0;
        }
        unset($info['negative_status']);
        Db::table('biz')->where(array('id' => $this->bizId))->update($info);
        # 完成每日设置员工提成任务
        TaskService::finishDayTask($this->bizId,3);
        return array('status' => true, 'msg' => '保存成功');
    }

    function bizUpgradeInfo()
    {
        $info = $this->bizUpgradeInfo_body();
        return array('status' => true, 'info' => $info);
    }

    function bizUpgradeInfo_body()
    {
        $info = array(
            'biz_id' => $this->bizId,//门店id
            'price' => '2000.00',//需支付金额
            'title' => '成为A类店',//项目名称
            'validity_days' => '6个月',//有效期
            'validity_days_num' => 6,//有效期(月)
            'cash_price' => '11.80',//每日返现
            'cash_days' => '6个月',//返现周期
        );
        $info['over_time'] = date('Y-m-d', strtotime('+' . $info['validity_days_num'] . ' months'));
        return $info;
    }

    function upgradePay()
    {
        # 支付方式 1微信 2支付宝
        $payType = input('post.payType');
        # 支付金额
        $pay_price = 2000;
        if($this->bizId==693){
            $pay_price = 0.01;
        }
        if (empty($payType)) {
            return array('status' => false, 'msg' => '请选择支付方式');
        }
        # 订单编号
        $orderNumber = date("YmdHis") . $this->bizId . time();
        $payMent = new Payment();
        if($payType==1){
            # 微信支付
            $order = [
                'out_trade_no' => $orderNumber,
                'total_fee' => $pay_price*100, // **单位：分**
                'body' => '成为A类店',
                'attach' => json_encode(array(
                    'biz_id'=>$this->bizId,
                    'over_time'=>$this->bizUpgradeInfo_body()['over_time']
                ))
            ];
            $payMent->wxPayAPP($order)->send();
        }else{
//            $pay_price = 0.01;
            # 支付宝支付
            $order = [
                'out_trade_no' => $orderNumber,
                'total_amount' => $pay_price,
                'subject' => '成为A类店',
                'passback_params'=>urlencode(json_encode(array(
                    'biz_id'=>$this->bizId,
                    'over_time'=>$this->bizUpgradeInfo_body()['over_time']
                )))  //回传参数-必须进行 urlEncode
            ];
            $payMent->aliPayAPP($order)->send();
        }
    }
}